package com.scrawler.mysql.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import com.scrawler.util.MysqlConfig;
import com.scrawler.vo.Keyword;
import com.scrawler.vo.NewsContentConfig;
import com.scrawler.vo.WebConfig;




/**  
 * @Description: 描述
 * @author kuntang    
 * @version 1.0  
 * @created 2012-11-2 下午4:39:48 
 * Copyright (c) 2012 sohu,Inc. All Rights Reserved.
 */

@Service
public class WebconfigSql {
	
	 private static final Log LOG = LogFactory.getLog(WebconfigSql.class);
	 
	 /**
	  * @Description: 返回最近24小时内不同缩略图url的集合
	  * @Date: 2013-1-6
	  * @param: @param channelId
	  * @param: @return
	  * @return: Set<String>
	  */
	 public static List<WebConfig> getWebConfig(){
		 Connection conn = MysqlConfig.getConnection();
		 String sql = "select * from index_search_config";
		 LOG.info(sql);
		 List<WebConfig> configs = new ArrayList<WebConfig>();
		 Statement st = null;
		try {
			st = conn.createStatement();
			st.execute(sql);
			ResultSet rs = st.getResultSet();
			while(rs.next()){
				WebConfig webconfig = new WebConfig();
				webconfig.setEnconding(rs.getString("encoding"));
				webconfig.setName(rs.getString("web_name"));
				webconfig.setUrl(rs.getString("address"));
				webconfig.setId(rs.getInt("id"));
				webconfig.setWeb_type(rs.getInt("web_type"));
				webconfig.setCrawlDepth(rs.getInt("crawl_depth"));
				webconfig.setNextPage(rs.getString("next_page"));
				webconfig.setTagName(rs.getString("hasTagFilter"));
				webconfig.setPreUrl(rs.getString("preUrl"));
				webconfig.setFilter(rs.getString("filter"));
				configs.add(webconfig);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			LOG.error("select pic url error.....",e);
		}finally{
			MysqlConfig.close(conn, st);
		}
		 return configs;
	 }
	 
	 
	 public static Map<Integer,NewsContentConfig> getContentConfig(){
		 Connection conn = MysqlConfig.getConnection();
		 String sql = "select * from second_content_config";
		 LOG.info(sql);
		 Map<Integer,NewsContentConfig> configs = new HashMap<Integer,NewsContentConfig>();
		 Statement st = null;
		try {
			st = conn.createStatement();
			st.execute(sql);
			ResultSet rs = st.getResultSet();
			while(rs.next()){
				NewsContentConfig newsConfig = new NewsContentConfig();
				int typeId = rs.getInt("type_id");
				newsConfig.setAuthor(rs.getString("author"));
				newsConfig.setComments(rs.getString("comments"));
				newsConfig.setContent(rs.getString("content"));
				newsConfig.setEncoding(rs.getString("encoding"));
				newsConfig.setHits(rs.getString("hits"));
				newsConfig.setPub_time(rs.getString("pub_time"));
				newsConfig.setTitle(rs.getString("title"));
				newsConfig.setTypeId(typeId);
				newsConfig.setWebsite_name(rs.getString("website_name"));
				configs.put(typeId,newsConfig);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			LOG.error("select pic url error.....",e);
		}finally{
			MysqlConfig.close(conn, st);
		}
		 return configs;
	 }
	 
	 public static Map<Integer,NewsContentConfig> getForumContentConfig(){
		 Connection conn = MysqlConfig.getConnection();
		 String sql = "select * from forum_content_config";
		 LOG.info(sql);
		 Map<Integer,NewsContentConfig> configs = new HashMap<Integer,NewsContentConfig>();
		 Statement st = null;
		try {
			st = conn.createStatement();
			st.execute(sql);
			ResultSet rs = st.getResultSet();
			while(rs.next()){
				NewsContentConfig newsConfig = new NewsContentConfig();
				int typeId = rs.getInt("type_id");
				newsConfig.setAuthor(rs.getString("author"));
				newsConfig.setComments(rs.getString("comments"));
				newsConfig.setContent(rs.getString("content"));
				newsConfig.setEncoding(rs.getString("encoding"));
				newsConfig.setHits(rs.getString("hits"));
				newsConfig.setPub_time(rs.getString("pub_time"));
				newsConfig.setTitle(rs.getString("title"));
				newsConfig.setTypeId(typeId);
				newsConfig.setWebsite_name(rs.getString("website_name"));
				configs.put(typeId,newsConfig);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			LOG.error("select pic url error.....",e);
		}finally{
			MysqlConfig.close(conn, st);
		}
		 return configs;
	 }
	 
	 
	/**
	 * @Description:
	 * @Date: 2012-11-2
	 * @param: @param args
	 * @return: void
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Map<Integer,NewsContentConfig> keywords = WebconfigSql.getForumContentConfig();
		for(NewsContentConfig keyword : keywords.values()){
			System.out.println(keyword.getEncoding());
		}
	}

}
